
package com.hlkj.pay.app.merchant.ext.impl.helipay.util;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.io.IOException;
import java.security.cert.X509Certificate;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.ext.impl.helipay.dto.HeliPayConfig;
import com.hlkj.pay.util.CommonSignUtil;
import com.hlkj.pay.util.SM2Utils;

/**
 * @author HlpayTeam
 * @date 2024/12/27 9:30
 */
public class HeliPaySM2Utils {

    // public static Set<String> payOrderSignFields;
    //
    // public static Set<String> refundOrderSignFields;
    // public static Set<String> settleOrderSignFields;

    /**
     *
     * @param response
     * @param heliPayConfig
     * @param type
     *            1 支付回调 2 退款回调
     * @return
     * @throws IOException
     */
    public static boolean verify(String response, HeliPayConfig heliPayConfig, Set<String> signFields) throws IOException {
        X509Certificate x509Certificate = CommonSignUtil.getX509Certificate(heliPayConfig.getPlatPublicKeyPath());
        Map<String, String> respMap = JsonUtils.parseObject(response, Map.class);
        final String resSign = respMap.remove("sign").toString();
        Map<String, String> map = new LinkedHashMap<>();
        signFields.stream().forEach(key -> {
            map.put(key, respMap.get(key));
        });
        String strAppend = HeliPaySignUtil.doSignStrAppend(map);
        return SM2Utils.verify(strAppend, resSign, x509Certificate);
    }
}
